package com.lnj.controller;

import com.lnj.config.AiConfig;
import com.lnj.service.ProductSearchService;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.service.TokenStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

import java.util.List;

@RestController
@RequestMapping("/ai")
public class ChatController {
    @Autowired
    private ChatModel chatModel;

    @Autowired
    private StreamingChatModel streamingChatModel;

    @GetMapping("/chat")   //  http://localhost:8080/ai/chat?message=请介绍一下自己
    public String model(@RequestParam("message") String message) {
        String answer = chatModel.chat(message);
        return answer;
    }

    @RequestMapping("/streamingchat")  // 在控制台上打印并不会打印在页面 http://localhost:8080/ai/streamingchat?question=请介绍一下自己
    public void streamingchat(@RequestParam("question") String question) {
        streamingChatModel.chat(question, new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String s) {
                System.out.print( s );
            }
            @Override
            public void onCompleteResponse(ChatResponse chatResponse) {
                System.out.println( chatResponse.toString());
            }
            @Override
            public void onError(Throwable e) {
                System.out.println(e.toString() );
            }
        });

    }

    //高级别的
    @Autowired
    private AiConfig.Assistant assistant;
    @Autowired
    private ProductSearchService productSearchService;

    @RequestMapping("/assistantchat")
    public String assistantchat(   @RequestParam("memoryId") String memoryId, @RequestParam("question")  String question ){
        String answer=assistant.chat(    memoryId,      question );
        return answer;
    }
    @GetMapping("/search")
    public ResponseEntity<List<String>> searchProducts(@RequestParam("keyword") String keyword) {
        List<String> productIds = productSearchService.searchProductIdsByKeyword(keyword);
        return ResponseEntity.ok(productIds);
    }

    //   http://localhost:9090/ai/assistantstreamingchat?memoryId=xxx&question=xxx
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.GET },value="/assistantstreamingchat", produces="text/stream;charset=utf-8")
    public Flux<String> assistantstreamingchat(@RequestParam("memoryId") String memoryId, @RequestParam("question") String question){
        TokenStream tokenStream=assistant.chatStream(  memoryId, question );

        return Flux.create(  fluxSink->{
            tokenStream.onPartialResponse(response->{
                fluxSink.next(   response);  // DATA: xx
            }).onCompleteResponse( response->{
                fluxSink.complete();
            }).onError(    error->{
                fluxSink.error(   error );
            }).start();
        } , FluxSink.OverflowStrategy.BUFFER );   //确保多线程安全的机制
    }

}
